Kivy:如何使用 RecycleView 在 Kivy 中顯示具有可變行的列表? (Kivy: how to display a list with variable rows in Kivy with a RecycleView?)


問題描述

Kivy:如何使用 RecycleView 在 Kivy 中顯示具有可變行的列表? (Kivy: how to display a list with variable rows in Kivy with a RecycleView?)

我是 Python 和 Kivy 的新手,我需要顯示一個包含可變行的列表,我正在嘗試使用回收視圖,但我認為我做錯了,我不知道它是怎麼做到的完全有效。這是我的代碼

class Introduccion(Screen):
numbers = ObjectProperty()
number_list = ObjectProperty([])

def Add_ToList(self):
    dat = self.numbers.text
    self.number_list.append([dat])

和我的.kv文件

<Introduccion>:
    numbers: numbers_input
    number_list: number_list_view
    RelativeLayout:
        pos_hint:{'center_y': 0.8, 'center_x':0.5}
        size_hint: None, None
        size: 700, 200
        pos: 200, 100
        canvas.before:
            Color:
                rgba: 1, 0.1, 0.2, 0
            Rectangle:
                pos: 0,0
                size: self.size
        TextInput:
            id: numbers_input
            size_hint: None, None
            pos: 520, 66
            size: 70, 30
            multiline: False
        Button:
            size_hint: None, None
            pos: 600, 66
            size: 50, 30
            text: "añadir"
            on_press: root.Add_ToList()
    BoxLayout:
        pos_hint:{'center_y': 0.8, 'center_x':0.5}
        size_hint: None, None
        size: 10, 10
        pos: 200, 100
        RecycleView:
            id: number_list_view
            viewclass: 'Label'
            RecycleBoxLayout:
                default_size: None, dp(26)
                default_size_hint: 1, None
                size_hint_y: None
                height: self.minimum_height
                orientation: 'vertical'

參考解法

方法 1:

The data of a RecycleView is a list of dictionaries with viewclass attributes as keys and the value of that attribute as values. Since your viewclass is Label, an appropriate key in the data dictionary is text. So your Introduccion class can be:

class Introduccion(Screen):
    numbers = ObjectProperty()
    number_list = ObjectProperty()

    def Add_ToList(self):
        self.number_list.data.append({'text': str(self.numbers.text)})

The Add_ToList() method adds a new dictionary to the RecycleView data on each call. In each case, the key is text (referring to the text attribute of Label), and the value is whatever is in the TextInput.

(by juan sebastianJohn Anderson)

參考文件

  1. Kivy: how to display a list with variable rows in Kivy with a RecycleView? (CC BY‑SA 2.5/3.0/4.0)

#listview #Python #android-recyclerview #list #kivy






相關問題

ListView中的序列號列 (Serial number Column in ListView)

狀態列表可繪製在預蜂窩版本上無法正常工作 (State list drawable not working properly on pre-honeycomb versions)

我可以減少列表視圖的執行時間嗎? (Can i reduce execution time of listview?)

ListView Windows 8 多個索引 (ListView Windows 8 multiple indexes)

如何通過單擊歌曲列表來播放歌曲 (How to play the song by clicking the lists of song)

Thuộc tính chi tiết trong ListView không hiển thị (Details property in ListView not displaying)

如何從 DataModel 動態訪問數據以獲取 Blackberry 10 中的可折疊列表 (How to access data dynamically from DataModel for collapsible list in Blackberry 10)

android如何將listview項目發送到另一個活動 (android How to send listview item to another activity)

實現 RecyclerView (Implementing RecyclerView)

如何刪除列表視圖底部的多餘空間? (How can I remove extraspace in listview bottom?)

ListViewItem ItemSelectionChangedEvent 觸發 4 次 [e.Selected 觸發兩次] 導致 Win32 異常未處理 (ListViewItem ItemSelectionChangedEvent Fires 4 times [e.Selected fires twice] leads to Win32 Exception Unhandled)

Kivy:如何使用 RecycleView 在 Kivy 中顯示具有可變行的列表? (Kivy: how to display a list with variable rows in Kivy with a RecycleView?)







留言討論